/**
 * File name: checkStatus.java
 * Classe: checkStatus
 * Goals: Searching student submission status and output details
 * @author Tuan Huynh
 * @version 1.00 
 * Create date: 04/05/09
 */
 
import java.io.*;
 
public class checkStatus
{
    public static void main(String[] args)
    {
        File file = new File("test.txt");
        StringBuffer contents = new StringBuffer();
        String [] Parameters =null;
        BufferedReader reader = null;
        String fileContent = null;
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        
 		try
    	{
    			while (System.in.available() > 0) //check enter
                	System.in.read(); //flush the buffer
                
                System.out.println("Enter studentId subjectId assignmentID");
                System.out.print(">");	
     			String searchStr=inFromUser.readLine(); // wait for user input
     			reader = new BufferedReader(new FileReader(file));
     			
     			if(!searchStr.equalsIgnoreCase("exit")) //if user doesn't type 'exit'
     			{
     				Parameters=searchStr.split("\\s"); //get each parameter and put in an array
     				if(Parameters.length!=3) //whether user entered 3 parameters?
     				{     				
     					System.out.println("insufficient parameters."); //if not, notify and exit
     					System.exit(1);
     				}
     			}     		
     			
     			//Reading the file content, repeat until all lines is read
            	while ((fileContent = reader.readLine()) != null)
            	{
            		//extract the content to compare to the input
                	String [] contentStr= fileContent.split("\\s"); 
                	String theText=contentStr[0].toString()+" "
                		+contentStr[1].toString()+" "
                			+contentStr[2].toString();
                	
                	//whether the searching is matched
         			if(theText.equalsIgnoreCase(searchStr))
         			{
         				System.out.println("StudentID	SubjectID	AssignmentID	Status");
         				System.out.println("--------------------------------------------------");
         				System.out.print(contentStr[0].toString()+"		"
         									+contentStr[1].toString()+"		"
         										+contentStr[2].toString()+"		"
         											+contentStr[3].toString()); //output details
         				if(contentStr.length==5)
         					System.out.print(" "+contentStr[4].toString());
         										
         				System.exit(1);
         			}
	            }
	            System.out.println("Could not find any detail matched to your search.");
     	        
        } //end try
        catch (FileNotFoundException e)
        {
            System.out.println("File is not found.");
        } catch (IOException e)
        {
            e.printStackTrace();
        } 
    }
}